package news.paper.tintuc.tinnong.docbao.doctin;

import news.paper.tintuc.tinnong.docbao.doctin.GCM.PopupAD;
import news.paper.tintuc.tinnong.docbao.doctin.controller.Controller;
import news.paper.tintuc.tinnong.docbao.doctin.controller.RequestListener;
import news.paper.tintuc.tinnong.docbao.doctin.model.News;
import news.paper.tintuc.tinnong.docbao.doctin.model.NewsDetail;
import news.paper.tintuc.tinnong.docbao.doctin.util.Constrant;

import org.json.JSONObject;

import tienich360.com.lib.JsonConvert;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.webkit.WebSettings;
import android.webkit.WebSettings.LayoutAlgorithm;
import android.webkit.WebView;

import com.startapp.android.publish.Ad;
import com.startapp.android.publish.AdEventListener;
import com.startapp.android.publish.StartAppAd;
import com.startapp.android.publish.StartAppSDK;
import com.startapp.android.publish.banner.bannerstandard.BannerStandard;

/**
 * An example full-screen activity that shows and hides the system UI (i.e.
 * status bar and navigation/system bar) with user interaction.
 * 
 * @see SystemUiHider
 */
@SuppressLint("SetJavaScriptEnabled")
public class ActivityNewsDetail extends Activity {
	private WebView contentView;
	private News news;
	private StartAppAd startAppAd = new StartAppAd(this);

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_news_detail);
		contentView = (WebView) findViewById(R.id.webview_new);
		news = (News) getIntent().getSerializableExtra("NEWS");
		contentView.getSettings().setJavaScriptEnabled(true);
		contentView.getSettings().setLayoutAlgorithm(
				LayoutAlgorithm.SINGLE_COLUMN);
		load_data(news.getContentID() + "", news.getListType() + "",
				news.getListId() + "");
		WebSettings webSettings = contentView.getSettings();
		webSettings.setDefaultFontSize(Constrant.TEXT_SIZE);
		if (PopupAD.ShowAd == 1) {
//			BannerStandard banner = (BannerStandard) findViewById(R.id.startAppBanner);
//			banner.setVisibility(View.VISIBLE);
			Constrant.showad++;
			if (Constrant.showad % 3 == 0) {
				StartAppSDK.init(this, "105853845", "210033017", true);
				startAppAd.loadAd(new AdEventListener() {

					@Override
					public void onReceiveAd(Ad arg0) {
						startAppAd.showAd();
					}

					@Override
					public void onFailedToReceiveAd(Ad arg0) {
					}
				});
			}
		}else{
			BannerStandard banner = (BannerStandard) findViewById(R.id.startAppBanner);
			banner.hideBanner();
		}

		// RelativeLayout mainLayout = (RelativeLayout)
		// findViewById(R.id.second_layout);
		//
		// // Create new StartApp banner
		// Banner startAppBanner = new Banner(this);
		// RelativeLayout.LayoutParams bannerParameters =
		// new RelativeLayout.LayoutParams(
		// RelativeLayout.LayoutParams.WRAP_CONTENT,
		// RelativeLayout.LayoutParams.WRAP_CONTENT);
		// bannerParameters.addRule(RelativeLayout.CENTER_HORIZONTAL);
		// bannerParameters.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
		//
		// // Add the banner to the main layout
		// mainLayout.addView(startAppBanner, bannerParameters);
	}

	@Override
	public void onResume() {
		super.onResume();
		startAppAd.onResume();
	}

	@Override
	public void onPause() {
		super.onPause();
		startAppAd.onPause();
	}

	public void load_data(String articleId, String listType, String listId) {
		Controller controller = new Controller();
		controller.getNewsDetail(articleId, listType, listId,
				new RequestListener() {

					@Override
					public void success(Object response) {
						try {
							JSONObject jsonObject = (JSONObject) response;
							JSONObject jsonObject2 = jsonObject
									.getJSONObject("article");
							NewsDetail newsDetail = JsonConvert
									.DeserializeObject(NewsDetail.class,
											jsonObject2);
							String string = "<b>"
									+ newsDetail.getTitle()
									+ "</b>"
									+ "<p>"
									+ "Nguồn: "
									+ newsDetail.getSourceName()
									+ " - "
									+ newsDetail.getDate()
									+ "<p>"
									+ newsDetail.getDescription()
									+ "<br>"
									+ newsDetail.getBody()
											.replace("src=\"_\"", "")
											.replace("data-img-src=", "src=");
							// .replace("data-img-width", "width")
							// .replace("data-img-height", "height");
							contentView.loadDataWithBaseURL(null, new String(
									string.getBytes("utf-8")), "text/html",
									"utf-8", null);
						} catch (Exception e) {
							e.printStackTrace();
						}
					}

					@Override
					public void fail(String message) {
					}

					@Override
					public void begin() {
					}
				});
	}
}
